package com.yygo.dao.hibernate;

import com.yygo.dao.VehicleSeriesDao;
import com.yygo.model.VehicleModel;
import com.yygo.model.VehicleSeries;
import com.yygo.model.enums.CarType;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by tiansha on 2015/6/23.
 */
@Repository("vehicleSeriesDao")
public class VehicleSeriesDaoHibernate extends GenericDaoHibernate<VehicleSeries, Long> implements VehicleSeriesDao {

    public VehicleSeriesDaoHibernate() {
        super(VehicleSeries.class);
    }

    @Override
    public Set<VehicleModel> getMyModels(String seriesName, boolean isSecond) {
        Criteria criteria = getSession().createCriteria(VehicleModel.class);
        List<CarType> carTypes = new ArrayList<CarType>();
        if (isSecond) {
            carTypes.add(CarType.new_and_old);
            carTypes.add(CarType.old_car);
        } else {
            carTypes.add(CarType.new_and_old);
            carTypes.add(CarType.new_car);
        }
        criteria.add(Restrictions.in("carType", carTypes));
        criteria.createAlias("vehicleSeries", "series").add(Restrictions.eq("series.name", seriesName));
        List models = criteria.list();

        return new HashSet<VehicleModel>(models);

    }
}
